package com.flot.yazb.base;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.IBinder;
import android.os.Looper;
import android.os.MessageQueue;
import android.support.annotation.AnimRes;
import android.support.annotation.IdRes;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.util.DisplayMetrics;
import android.view.MotionEvent;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;

import com.flot.yazb.R;
import com.flot.yazb.CUIAgent;
import com.flot.yazb.loading.ProgressManager;
import com.flot.yazb.widget.utils.AppManager;
import com.flot.yazb.widget.utils.StatusBarUtils;
import com.flot.yazb.widget.utils.TestInject;
import com.umeng.analytics.MobclickAgent;

/**
 * Created by fengdongfei on 2017/8/24.
 */

public class BActivity extends AppCompatActivity implements ProgressManager {
    public BActivity activity;
    public int screenHeight;
    public int screenWidth;

    private String mPageName =this.getClass().getSimpleName();
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        CUIAgent.fixFontScale(this);
        testInject();
        activity = this;
        DisplayMetrics dm = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(dm);
        screenHeight = dm.heightPixels;
        screenWidth = dm.widthPixels;

        AppManager.getAppManager().addActivity(this);
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        BApplication.removeActivity(this);
        CUIAgent.clearCache(this);
    }


    private void testInject() {
        Looper.myQueue().addIdleHandler(new MessageQueue.IdleHandler() {
            @Override
            public boolean queueIdle() {
                TestInject annotation = BActivity.this.getClass().getAnnotation(TestInject.class);
                if (annotation != null) {
                    if (annotation.value() != null) {
                        startActivity(new Intent(BActivity.this, annotation.value()));
                    }
                }
                return false;

            }
        });

    }

    @Override
    public void showProgress() {
        CUIAgent.showProgress(this);
    }

    @Override
    public void dismissProgress() {
        CUIAgent.dismissProgress(this);
    }

    public void replaceFragment(int id_content, Fragment fragment) {
        replaceFragment(id_content, 0, 0, fragment);
    }

    public void replaceFragment(@IdRes int id_content, @AnimRes int anim_in, @AnimRes int anim_out, Fragment fragment) {
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        transaction.replace(id_content, fragment);
        transaction.commit();
    }

    public void pushFragment(int resId, BFragment fragment) {
        String tag = fragment.getClass().getSimpleName();
        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        ft.addToBackStack(tag).add(resId, fragment, tag).commitAllowingStateLoss();
    }

    public void popFragment() {
        popFragment(true);
    }

    public void popFragment(boolean notify) {
        onBackPressed(notify);
    }

    @Override
    public void onBackPressed() {
        onBackPressed(true);
    }

    public void onBackPressed(boolean notify) {
        super.onBackPressed();
    }

    protected void onResume() {
        super.onResume();
        CUIAgent.fixFontScale(this);
        StatusBarUtils.setStatusBarTranslucent(this, true, R.color.theme_color, false);

        MobclickAgent.onPageStart(mPageName);
        MobclickAgent.onResume(this);
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        if (ev.getAction() == MotionEvent.ACTION_DOWN) {
            View v = getCurrentFocus();
            if (isShouldHideKeyboard(v, ev)) {
                hideKeyboard(v.getWindowToken());
            }
        }
        return super.dispatchTouchEvent(ev);
    }

    private boolean isShouldHideKeyboard(View v, MotionEvent event) {
        if (v != null && (v instanceof EditText)) {
            int[] l = {0, 0};
            v.getLocationInWindow(l);
            int left = l[0],
                    top = l[1],
                    bottom = top + v.getHeight(),
                    right = left + v.getWidth();
            if (event.getX() > left && event.getX() < right
                    && event.getY() > top && event.getY() < bottom) {
                // 点击EditText的事件，忽略它。
                return false;
            } else {
                return true;
            }
        }
        return false;
    }

    private void hideKeyboard(IBinder token) {
        if (token != null) {
            InputMethodManager im = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
            im.hideSoftInputFromWindow(token, InputMethodManager.HIDE_NOT_ALWAYS);
        }
    }


    @Override
    protected void onPause() {
        super.onPause();
        MobclickAgent.onPageEnd(mPageName);
        MobclickAgent.onPause(this);
    }

}